// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/brn/brn_select_tag.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/extensions.dart';
import 'filter_dialog.dart';

class FilterStatusTags<T> extends StatefulWidget {
  /// 标题
  String? title;

  /// 可选择的tag列表
  List<T> tagList;

  /// 已选中的tag
  List<T> selectTags;

  /// 是否单选
  bool isSingleSelect;
  final double? tagWidth;
  final double? spacing;
  //选中回调
  void Function(List<T>?)? onSelect;

  /// 单选回调
  void Function(
    int,
    T?,
  )? onSingleSelect;

  String Function(T) labelName;

  FilterStatusTags(this.tagList,
      {super.key,
      required this.labelName,
      this.title,
      this.tagWidth,
      this.spacing,
      this.onSelect,
      this.onSingleSelect,
      this.isSingleSelect = true,
      this.selectTags = const []});

  @override
  State<FilterStatusTags<T>> createState() => _FilterStatusTagsState<T>();
}

class _FilterStatusTagsState<T> extends State<FilterStatusTags<T>> {
  int? singleSelectIndex;
  List<bool>? multiSelectState;
  getSelected() {
    if (widget.selectTags.isEmpty) {
      if (widget.isSingleSelect) {
        singleSelectIndex = 0;
      } else {
        multiSelectState = widget.tagList.mapIndexed((e, i) => false).toList();
        multiSelectState?.insert(0, false);
      }
    } else {
      if (widget.isSingleSelect) {
        singleSelectIndex = widget.tagList.indexOf(widget.selectTags[0]) + 1;
      } else {
        var stateList =
            List.generate(widget.tagList.length + 1, (index) => null)
                .map((e) => false)
                .toList();
        for (var e in widget.selectTags) {
          var index = widget.tagList.indexOf(e);
          if (index > -1) {
            stateList[index + 1] = true;
          }
        }

        if (widget.selectTags.length == widget.tagList.length) {
          stateList[0] = true;
        }
        multiSelectState = stateList;
      }
    }
  }

  @override
  void initState() {
    getSelected();
    super.initState();
  }

  @override
  void didUpdateWidget(covariant FilterStatusTags<T> oldWidget) {
    if (widget.selectTags != oldWidget.selectTags ||
        widget.tagList != oldWidget.tagList) {
      getSelected();
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        /// 标题
        if (widget.title != null) FilterDialogWidget.pickTitle(widget.title!),

        /// 选中的标签
        YtBrnSelectTag(
          key: UniqueKey(),
          tagWidth: widget.tagWidth ?? 104.w,
          tagHeight: 32.h,
          spacing: widget.spacing ?? 10.w,
          tagBackgroundColor: Colour.backgroundColor,
          tags: [
            ResKey.all.tr,
            ...widget.tagList.map(
              (element) => widget.labelName(element),
            )
          ],
          isSingleSelect: widget.isSingleSelect,
          singleSelectIndex: singleSelectIndex,
          initTagState: multiSelectState,
          onSelect: (p0) {
            if (widget.isSingleSelect) {
              var index = p0[0];
              T? selectedTag = index == 0 ? null : widget.tagList[index - 1];
              List<T>? selectedTagList = index == 0 ? null : [selectedTag as T];
              widget.selectTags = [];
              widget.onSelect?.call(selectedTagList);
              widget.onSingleSelect?.call(index, selectedTag);

              return;
            }
            if (p0.isNotEmpty && p0[0] != 0 && multiSelectState?[0] == true) {
              // "全部"被取消了
              if (widget.isSingleSelect) {
              } else {
                multiSelectState =
                    List.generate(widget.tagList.length + 1, (index) => null)
                        .map((e) => false)
                        .toList();
                widget.selectTags = [];
              }
            } else if (p0.isNotEmpty &&
                multiSelectState?[0] == false &&
                p0[0] == 0) {
              //"全选"被选中

              multiSelectState = widget.tagList.map((e) => true).toList();
              multiSelectState?.insert(0, true);
              widget.selectTags = widget.tagList.map((e) => e).toList();
            } else {
              List<T> tags = [];
              var stateList =
                  List.generate(widget.tagList.length + 1, (index) => false);
              for (var index in p0) {
                if (index != 0) {
                  tags.add(widget.tagList[index - 1]);
                  stateList[index] = true;
                }
              }

              // 所有 tag 都被选中，则“全选”也被选中
              if (tags.length == widget.tagList.length) {
                stateList[0] = true;
              }
              multiSelectState = stateList;
              widget.selectTags = tags;
            }
            widget.onSelect?.call(widget.selectTags);
            setState(() {});
          },
        ),
      ],
    );
  }
}
